import {getItem, setItem} from "../services/LocalDataService";
import update from "react-addons-update";
import {getComUrl} from "../utils";
import {getUserInfo} from "../services/SecurityService";

const tabName: string = "tabList";
/**
 * tabs 模板
 * {
 *     label:必传，tab标题
 *     key:必传，唯一key
 *     isSelect:true 可空，是否选中
 *     url: 必传，路由路径
 * }
 */
const TabsReducer: object = {
    namespace: "tabsReducer",
    state: {
        tabMap: getItem(tabName) || {},
        userId: getUserInfo()?.id
    },
    addTabs: function (params: any) {
        // @ts-ignore
        const {data = {}}: { data: any } = params,
            {tabMap, userId}: { tabMap: any, userId: string } = this.state;
        let tabs: any[] = tabMap[userId] || [];
        data.url = getComUrl(data);
        data.children = null;
        let newTabs: object[];
        let curIndex: number = tabs.findIndex((item: any) => item.key === data.key);
        newTabs = tabs.map((item: object, index: number) => {
            return {
                ...item,
                isSelect: index === curIndex
            }
        });
        if (curIndex !== -1) {
            newTabs = update(newTabs, {$splice: [[curIndex, 1, data]]});
        } else {
            newTabs = update(newTabs, {$push: [data]});
        }
        setItem(tabName, {...tabMap, [userId]: newTabs});
        // @ts-ignore
        return update(this.state, {tabMap: {$set: {...tabMap, [userId]: newTabs}}});
    },
    updateTabs: function (params: any) {
        // @ts-ignore
        const {data = {}, key}: { data: any, key: string } = params,
            {tabMap, userId}: { tabMap: any, userId: string } = this.state;
        let tabs: any[] = tabMap[userId] || [];
        data.url = getComUrl(data);
        data.children = null;
        let curIndex: number = tabs.findIndex((item: any) => item.key === key);
        let newTabs: object[] = update(tabs, {$splice: [[curIndex, 1, data]]});
        setItem(tabName, {...tabMap, [userId]: newTabs});
        // @ts-ignore
        return update(this.state, {tabMap: {$set: {...tabMap, [userId]: newTabs}}});
    },
    removeTabs: function (params: any) {
        // @ts-ignore
        const {keys = []}: { keys: string[] } = params,
            {tabMap, userId}: { tabMap: any, userId: string } = this.state;
        let tabs: any[] = tabMap[userId] || [], newTabs: object[] = [...tabs];
        keys.forEach((key: string) => {
            let curIndex: number = newTabs.findIndex((item: any) => item.key === key);
            if (curIndex !== -1) {
                newTabs = update(newTabs, {$splice: [[curIndex, 1]]});
            }
        });
        setItem(tabName, {...tabMap, [userId]: newTabs});
        // @ts-ignore
        return update(this.state, {tabMap: {$set: {...tabMap, [userId]: newTabs}}});
    },
    removeAllTabs: function () {
        const {tabMap, userId}: { tabMap: any, userId: string } = this.state;
        setItem(tabName, {...tabMap, [userId]: []});
        // @ts-ignore
        return update(this.state, {tabMap: {$set: {...tabMap, [userId]: []}}});
    }
};

export default TabsReducer;